#include <iostream>
#include <string>
#include <pthread.h>
bool ready = false;
pthread_mutex_t mutex;
pthread_cond_t cond;
int ticket = 0;
void *Consumer(void *args)
{
    std::string name = static_cast<const char *>(args);
    pthread_mutex_lock(&mutex);
    while (!ready)
    {
        std::cout << name << "资源等待中" << std::endl;
        pthread_cond_wait(&cond, &mutex);
    }
    // 处理资源
    std::cout << name << "资源成功消耗" << std::endl;
    pthread_mutex_unlock(&mutex);
    return nullptr;
}
void *Producer(void *args)
{
    std::string name = static_cast<const char *>(args);
    // 准备资源
    ready = true;
    std::cout << name << "资源准备完毕" << std::endl;
    pthread_cond_signal(&cond);
    return nullptr;
}
int main()
{
    pthread_mutex_init(&mutex, nullptr);
    pthread_cond_init(&cond, nullptr);
    pthread_t consumer,producer;
    pthread_create(&consumer,nullptr,Consumer,(void*)"consumer");
    pthread_create(&producer,nullptr,Producer,(void*)"prodecer");
    pthread_join(consumer,nullptr);
    pthread_join(producer,nullptr);
    pthread_mutex_destroy(&mutex);
    return 0;
}